﻿using System.Collections.Generic;
using Entitas;
using System.Linq;
class EnemyReturnToSpawnSystem :ReactiveSystem<CommandEntity>,ICleanupSystem
{
    EnemyContext enemyContext;
    CommandContext commandContext;
    public EnemyReturnToSpawnSystem(Contexts contexts) : base(contexts.command)
    {
        enemyContext = contexts.enemy;
        commandContext = contexts.command;
    }

    protected override void Execute(List<CommandEntity> entities)
    {
        foreach(var e in entities)
        {
            var enemy = enemyContext.GetEntityWithName(e.returnToSpawnCommand.Owner);
            var newpos = enemy.position.value;
            switch (e.returnToSpawnCommand.direction)
            {
                case Direction.Up:
                    newpos.y += enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMaxValue(newpos.y,enemy.spawnPosition.value.y);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Up_Left:
                    newpos.y += enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMaxValue(newpos.y, enemy.spawnPosition.value.y);
                    newpos.x -= enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMinValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Up_Right:
                    newpos.y += enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMaxValue(newpos.y, enemy.spawnPosition.value.y);
                    newpos.x += enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMaxValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Down:
                    newpos.y -= enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMinValue(newpos.y, enemy.spawnPosition.value.y);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Down_Left:
                    newpos.y -= enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMinValue(newpos.y, enemy.spawnPosition.value.y);
                    newpos.x -= enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMinValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Down_Right:
                    newpos.y -= enemy.speed.value;
                    newpos.y = EntitasVec3.ClampMinValue(newpos.y, enemy.spawnPosition.value.y);
                    newpos.x += enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMaxValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Left:
                    newpos.x -= enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMinValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                case Direction.Right:
                    newpos.x += enemy.speed.value;
                    newpos.x = EntitasVec3.ClampMaxValue(newpos.x, enemy.spawnPosition.value.x);
                    enemy.ReplacePosition(newpos);
                    break;
                default:
                    break;
            }
        }
    }

    protected override bool Filter(CommandEntity entity)
    {
        return entity.hasReturnToSpawnCommand;
    }

    protected override ICollector<CommandEntity> GetTrigger(IContext<CommandEntity> context)
    {
        return context.CreateCollector(CommandMatcher.ReturnToSpawnCommand.Added());
    }


    public void Cleanup()
    {
        foreach(var e in commandContext.GetEntities().Where(c => c.hasReturnToSpawnCommand == true))
        {
            e.Destroy();
        }
    }

}

